#!/usr/bin/perl
use strict; use warnings;

use lib 'lib';

use Bigtop::Keywords;
use Bigtop::TentMaker;

my @doc_hash_categories;

Bigtop::TentMaker::build_backend_list();

my $backends = Bigtop::TentMaker::get_backends();
my $output   = header() . chewy_center() . footer();

print $output;

sub chewy_center {
    my $retval;

    my @backend_types = sort keys %{ $backends };
    my %backend_names;
    my %backend_keywords;

    foreach my $backend_type ( @backend_types ) {
        my @backends = keys %{ $backends->{ $backend_type } };
        @backend_names{ @backends } = @backends;

        foreach my $backend_name ( @backends ) {
            my @keywords;

            foreach my $keyword_hash ( @{ $backends->{ $backend_type }
                                                     { $backend_name }
                                                     { keywords      } }
            ) {
                push @keywords, $keyword_hash->{ keyword };
            }

            @backend_keywords{ @keywords } = @keywords;
        }
    }

    my @backend_names    = sort keys %backend_names;
    my @backend_keywords = sort keys %backend_keywords;

$retval = <<"EO_BACKEND_STUFF";
syn keyword bigtopBackendTypes    @backend_types

syn keyword bigtopBackends        @backend_names

syn keyword bigtopBackendKeywords @backend_keywords

EO_BACKEND_STUFF

    my $keyword_for = Bigtop::Keywords->get_full_doc_hash();

    foreach my $category ( sort keys %{ $keyword_for } ) {

        my $pieces    = join '', map { ucfirst $_ } split /_/, $category;
        my $vim_label = "bigtop${pieces}Keywords";

        push @doc_hash_categories, $vim_label;

        my @keywords  = sort keys %{ $keyword_for->{ $category } };

        $retval .= "syn keyword $vim_label @keywords\n\n";
    }

    return $retval;
}

sub header {
    my $now = scalar localtime;
    return <<"EO_HEADER";
" Vim syntax file
" Language:    Bigtop (a language for describing database backed web apps)
" Maintaner:   Phil Crow <crow.phil\@gmail.com>
" Last Change: $now
" Filenames:   *.bigtop
"
" Note that this file was generated by skip/vimsyntax in the bigtop CPAN
" distribution.

if version < 600
    syntax clear
else
    if exists("b:current_syntax")
        finish
    endif
endif

syn case match

syn match  bigtopComment /^#.*/
syn region bigtopString start=+`+ end=+`+

syn keyword bigtopBlocks         block config app literal table field controller method sequence

syn keyword bigtopEngines        MP13 MP20 TT

syn keyword bigtopValues         no_gen no_accessor fast_cgi instance conffile with_server server_port gen_root flex_db full_use skip_config dbix model_base_class gantry_wrapper

syn keyword bigtopFieldValues    text textarea select display

syn keyword bigtopSQLKeywords    int4 varchar text boolean int primary_key assign_by_sequence auto

EO_HEADER
}

sub footer {
    my $hi_def_links = '';
    my $longest      = 40;
    my $space        = ' ';

    foreach my $category ( @doc_hash_categories ) {
        my $padding  = $space x ( $longest - length $category );
        $hi_def_links .= "hi def link ${category}${padding}Identifier\n";
    }

    return <<"EO_FOOTER";
hi def link bigtopComment                           Comment
hi def link bigtopString                            String

hi def link bigtopBlocks                            Keyword
hi def link bigtopEngines                           Constant
hi def link bigtopBackendTypes                      Keyword
hi def link bigtopBackends                          Identifier
hi def link bigtopBackendKeywords                   Identifier
hi def link bigtopLiteralTypes                      Identifier
hi def link bigtopMethodTypes                       Keyword

hi def link bigtopValues                            Identifier
hi def link bigtopFieldValues                       Identifier
hi def link bigtopSQLKeywords                       Identifier

$hi_def_links
if exists("bigtop_fold")
    syn region blockFold start="{" end ="}" transparent fold
    syn sync fromstart
    set foldmethod=syntax
endif
EO_FOOTER
}

=head1 NAME

vimsyntax - generates the vim syntax file for bigtop

=head1 SYNOPSIS

    skip/vimsyntax > vim/syntax/bigtop.vim

=head1 DESCRIPTION

This script uses C<Bigtop::Keywords> to generate a replacement vim
syntax highlighting file.

=head1 AUTHOR

Phil Crow

=cut
